package nc.task.hdysc;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import nc.bs.dao.BaseDAO;
import nc.bs.dao.DAOException;
import nc.bs.framework.common.NCLocator;
import nc.bs.pub.taskcenter.BgWorkingContext;
import nc.bs.pub.taskcenter.IBackgroundWorkPlugin;
import nc.itf.uap.bd.def.IDefdoc;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.jdbc.framework.processor.MapListProcessor;
import nc.vo.bd.def.DefdocVO;
import nc.vo.pub.BusinessException;

public class HotelRoomTypeTask implements IBackgroundWorkPlugin {

	private Map<String, DefdocVO> existsDocList;

	private IDefdoc idf;

	private String defDocListPK;

	private String defDocListCode = "CRS11";

	private String getDefDocListPK() throws BusinessException {
		if (defDocListPK == null) {
			BaseDAO dao = new BaseDAO();
			defDocListPK = (String) dao.executeQuery("select pk_defdoclist from bd_defdoclist where doclistcode='"
					+ defDocListCode + "'", new ColumnProcessor());
		}

		return defDocListPK;
	}

	private IDefdoc getDefDocService() {
		if (idf == null) {
			idf = NCLocator.getInstance().lookup(IDefdoc.class);
		}

		return idf;
	}

	private Map<String, DefdocVO> getExistsDocMap() throws BusinessException {
		if (existsDocList == null) {
			existsDocList = new HashMap<String, DefdocVO>();
			DefdocVO[] docs = getDefDocService()
					.queryByWhere(
							"pk_defdoclist in (select pk_defdoclist from bd_defdoclist where doclistcode='"
									+ defDocListCode + "')");

			if (docs == null) {
				return existsDocList;
			}
			for (int i = 0; i < docs.length; i++) {
				existsDocList.put(docs[i].getDoccode(), docs[i]);
			}
		}
		return existsDocList;
	}

	public String executeTask(BgWorkingContext bgwc) throws BusinessException {
		try {
			List<Map<String, String>> dataMap = getDataMap();

			Map<String, DefdocVO> defDocMap = getDefDocList(dataMap);

			insertOrUpdate(defDocMap);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	private void insertOrUpdate(Map<String, DefdocVO> defDocMap) throws BusinessException {
		Iterator<String> it = defDocMap.keySet().iterator();

		while (it.hasNext()) {
			String docCode = it.next();
			DefdocVO docVO = defDocMap.get(docCode);
			if (getExistsDocMap().containsKey(docCode)) {
				// update
				getExistsDocMap().get(docCode).setDocname(docVO.getDocname());
				getDefDocService().update(getExistsDocMap().get(docCode));
			} else {
				// insert
				docVO.setPk_defdoclist(getDefDocListPK());
				getDefDocService().insert(docVO);
			}
		}
	}

	private List<Map<String, String>> getDataMap() throws DAOException {
		List<Map<String, String>> resultList = new ArrayList<Map<String, String>>();
		BaseDAO dao = new BaseDAO("sql");
		StringBuffer sql = new StringBuffer();
		sql
				.append("select convert(varchar(10),id) doccode ,DName docname from tbdRoomType");
		resultList = (List<Map<String, String>>) dao.executeQuery(sql.toString(), new MapListProcessor());
		return resultList;
	}
	private Map<String, DefdocVO> getDefDocList(List<Map<String, String>> dataMap) {
		Map<String, DefdocVO> result = new HashMap<String, DefdocVO>();

		Iterator<Map<String, String>> it = dataMap.iterator();

		while (it.hasNext()) {
			Map<String, String> itemmap = it.next();
   
			// String pName = itemmap.get("pname") ;
			String docCode = itemmap.get("doccode");
			String docName = itemmap.get("docname");

			DefdocVO docVO = new DefdocVO();
			docVO.setDoccode(docCode);
			docVO.setDocname(docName);
			// docVO.setPk_defdoc1(pName) ;
			docVO.setDocsystype(1);
			docVO.setPk_corp("0001");
			result.put(docCode, docVO);
		}

		return result;
	}

}
